package de.backtomedieval.activities.gamemenu;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Spinner;
import de.backtomedieval.R;
import de.backtomedieval.activities.TopBar;
import de.backtomedieval.main.Game;


public class SettingsScreen extends Activity {

	private Handler mHandler = new Handler();
	private int refresh = Game.getRefreshrate();
	private TopBar topbar;
	
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settingsmenu);       
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN );
		
		
		
		topbar = new TopBar(this);
		topbar.setTitle(Game.getGame().getcCity().getName() + ": "+ getString(R.string.settings));	
		
		Button buttonSave= (Button) findViewById(R.id.ButtonSave);
		buttonSave.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				onButtonSave();
			}
		});
		
		

		Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		final CheckBox checkBoxPauseEndFight = (CheckBox) findViewById(R.id.pauseonendoffight);
		final CheckBox checkBoxPauseStartFight = (CheckBox) findViewById(R.id.pauseonstartoffight);
		final CheckBox checkBoxPauseStartGame = (CheckBox) findViewById(R.id.pauseongamestart);
		ArrayAdapter<?> adapter;
		if(!Game.DEBUG) {
			adapter = ArrayAdapter.createFromResource(this, R.array.gamespeed, android.R.layout.simple_spinner_item);
		} else {
			adapter = ArrayAdapter.createFromResource(this, R.array.gamespeed_debug, android.R.layout.simple_spinner_item);
		}		
		adapter.setDropDownViewResource(android.R.layout.select_dialog_singlechoice);
		s.setAdapter(adapter);

		SharedPreferences settings = getSharedPreferences(Game.PREFS_NAME, 0);
		int loadGameSpeed = settings.getInt("gamespeed", 2);
		if (loadGameSpeed>5) {
			loadGameSpeed=5;
		}
		s.setSelection(loadGameSpeed);
		boolean pauseOnEndOfFight = settings.getBoolean("pauseonendoffight", false);
		checkBoxPauseEndFight.setChecked(pauseOnEndOfFight);
		boolean pauseOnStartOfFight = settings.getBoolean("pauseonstartoffight", false);
		checkBoxPauseStartFight.setChecked(pauseOnStartOfFight);
		boolean pauseOnGameStart = settings.getBoolean("pauseongamestart", false);
		checkBoxPauseStartGame.setChecked(pauseOnGameStart);
		
		topbar.refreshCityInfo();
	}

		
	@Override
	protected void onStop(){
		super.onStop();

		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getSharedPreferences(Game.PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		final Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		final CheckBox checkBoxPauseEndFight = (CheckBox) findViewById(R.id.pauseonendoffight);
		final CheckBox checkBoxPauseStartFight = (CheckBox) findViewById(R.id.pauseonstartoffight);
		final CheckBox checkBoxPauseStartGame = (CheckBox) findViewById(R.id.pauseongamestart);
		
		
		editor.putInt("gamespeed", s.getSelectedItemPosition());
		editor.putBoolean("pauseonendoffight", checkBoxPauseEndFight.isChecked());
		editor.putBoolean("pauseonstartoffight", checkBoxPauseStartFight.isChecked());
		editor.putBoolean("pauseongamestart", checkBoxPauseStartGame.isChecked());
		// Commit the edits!
		editor.commit();
	}

	
	@Override
	public void onBackPressed() {
		final Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		Game.setGameSpeed(s.getSelectedItemPosition());
		Intent changeToIntent = new Intent(this,Gamemenu.class);
		startActivity(changeToIntent);
		this.finish();
		return;
	}
	


	public void onButtonSave() {
		final Spinner s = (Spinner) findViewById(R.id.newgame_gamespeed);
		final CheckBox checkBoxPauseEndFight = (CheckBox) findViewById(R.id.pauseonendoffight);
		final CheckBox checkBoxPauseStartFight = (CheckBox) findViewById(R.id.pauseonstartoffight);
		final CheckBox checkBoxPauseStartGame = (CheckBox) findViewById(R.id.pauseongamestart);
		Game.setGameSpeed(s.getSelectedItemPosition());
		Game.setPauseOnEndOfFight(checkBoxPauseEndFight.isChecked());
		Game.setPauseOnStartOfFight(checkBoxPauseStartFight.isChecked());
		Game.setPauseOnGameStart(checkBoxPauseStartGame.isChecked());
		Intent changeToIntent = new Intent(this,Gamemenu.class);
		startActivity(changeToIntent);
		this.finish();
	}
	
	
	@Override
	public void onPause() {
		super.onPause();
		mHandler.removeCallbacks(mUpdateCityInfoTask);
	}

	@Override
	public void onResume() {
		super.onResume();
		Game.setLastActivity(this);
		mHandler.removeCallbacks(mUpdateCityInfoTask);
		mHandler.postDelayed(mUpdateCityInfoTask, 100);
	}
	
	private Runnable mUpdateCityInfoTask = new Runnable() {
		public void run() {
			Log.v("Update", "Gamemenu: Cityinfo");
			topbar.refreshCityInfo();
			mHandler.postDelayed(this, refresh);

		}
	};

}
